<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Session;
use DB;

class UserController extends Controller
{
    //
    public function space(){
        $uid = Session('home') -> id;
        $res = DB::table('t_order') -> where('uid', $uid) -> get();
        $data = DB::table('t_order as o') -> Join('t_goods as g', 'g.gid','=','o.gid') -> Join('t_info as i','i.id','=','o.info_id')
                                          -> select('o.*','g.*','i.rname') -> whereIn('o.uid', [$uid]) -> get();
        // dd($data);
        // dd($res[0] -> order_num);
        $num = [];
        for ($i=0; $i < count($res) ; $i++) { 
            $num[$i] = $res[$i] -> order_num;
        }
        $num = array_unique($num);
        $num  = array_values($num);
        $orders = [];
        for ($i=0; $i <count($num) ; $i++) { 
            $orders[$num[$i]] = DB::table('t_order') -> where('order_num',$num[$i]) -> get();

        }
        // dd($orders);
        // dd($num);
        // dd($res[0] -> gid);
        // $gid = $res[0] -> gid;
        // $shop = DB::table('t_goods') -> where('gid', $gid) -> 
        // dd($gid);
        return view('home.user.space',['title' => '个人中心', 'res' => $res, 'data' => $data, 'num' => $num, 'orders' => $orders]);
    }
    public function detail(){
        return view('home.user.detail');
    }
    public function update(Request $request){
        $haha = $request -> except('_token');
        $id = session('home') -> id;
        $request['id'] = $id;
       // dd($request -> all());
       $data = DB::table('users')
            ->where('email',$request->email)
            ->get();
        if($data){
            return 2;
        }

         //获取原来图片
        $oldPic = DB::table('users') -> where('id',$id) -> first() -> pic;
        // dd($oldPic);
        // dd($request);
        // dd($request -> hasFile('pic'));
        if($request -> hasFile('pic'))
        {
            if($request -> file('pic') -> isValid())
            {
                $suffix = $request -> file('pic') -> getClientOriginalExtension();
                $fileName = time().mt_rand(10000,99999).'.'.$suffix;
                //移动文件
                $move = $request -> file('pic') -> move('./uploads/yhtx',$fileName);

                if($move)
                {
                    $haha['pic'] = $fileName;
                    //删除原来图片
                    // unlink('./uploads/yhtx/'.$oldPic);
                    
                }
            }
        }
        //执行修改
        $res = DB::table('users') -> where('id', $id) -> update($haha);
        if($res)
        {
            return redirect('/login');
        }else
        {
            return back();
        }
    }
    public function address(){

        $data = DB::table('t_info')
            -> where('uid',Session('home')->id)
            -> get();
            // dd($data);
            // $data=$request;
        return view('home.user.address',['data'=>$data]);
    }
    public function doAddress(Request $request){
        $data = $request -> except('_token');
        $data['uid']=Session('home')->id;
        $data = DB::table('t_info')
            -> insert($data);
        return back();
    }
    public function delAddress(Request $request){
        $data = DB::table('t_info')
            ->where('id',$request->id)
            ->delete();
        return back();
    }

    public function status(Request $request)
    {
        $order_num = $request ->only('order_num');

        $data = DB::table('t_order') -> where('order_num', $order_num) -> update(['order_status' => '2']);
        // return 111;
        // var_dump($data);
        if($data)
        {
            return 2;
        }

    }
    // 展示收藏
    public function favorite(){
        $data = DB::table('t_favorite as f')
            -> join('t_goods as g','f.gid','=','g.gid')
            -> where('f.uid',session('home')->id)
            -> get();
        $arr = [];
        foreach($data as $u){
            foreach($u as $k=>$v){
                $arr[$u->gid][$k]=$v;
            }
            $r = DB::table('t_photo')
                -> where('gid',$u->gid)
                -> where('small','1')
                -> first();
            $photo = $r ? $r->photo : '';
            $arr[$u->gid]['pic']=$photo;
        }
        // dd($arr);
        return view('home.user.favorite',['data'=>$arr]); 
    }
    // 添加收藏
    public function addFavorite(Request $request){
        $result = DB::table('t_favorite')
            -> where('uid',session('home')->id)
            -> whereIn('gid',['gid'=>$request->only('gid')])
            ->first();
            // dd($request->gid);
        if($result){
            return 1;
        }else{
            $res = DB::table('t_favorite')
                -> insert(['uid'=>session('home')->id,'gid'=>$request->gid]);
            return $res ? 1 : false; 
        }
    }
    // 删除收藏
    public function delFavorite(Request $request){
        // dd($request->id);
        $res = DB::table('t_favorite')
            -> where('uid',session('home')->id)
            -> where('gid',$request->id)
            ->delete();
        return back();
    }

}
